/**
 * iSocial Project
 * http://isocial.missouri.edu
 *
 * Copyright (c) 2011, University of Missouri iSocial Project, All Rights Reserved
 *
 * Redistributions in source code form must reproduce the above
 * copyright and this condition.
 *
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 *
 * The iSocial project designates this particular file as
 * subject to the "Classpath" exception as provided by the iSocial
 * project in the License file that accompanied this code.
 */
package org.jdesktop.wonderland.modules.isocial.generic.client;

import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JCheckBox;
import org.jdesktop.wonderland.modules.isocial.generic.common.GenericAnswer;

/**
 * The panel to show multiple choice inclusive type questions (check box button).
 *
 * @author Kaustubh
 */
public class MCQInclusivePanel extends javax.swing.JPanel {

    /** Creates new form MCQInclusivePanel */
    public MCQInclusivePanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        textFieldPanel = new javax.swing.JPanel();
        questionField = new javax.swing.JScrollPane();
        questionArea = new javax.swing.JTextArea();
        buttonPanel = new javax.swing.JPanel();

        setPreferredSize(new java.awt.Dimension(400, 310));

        textFieldPanel.setLayout(new java.awt.BorderLayout());

        questionArea.setBackground(new java.awt.Color(238, 238, 238));
        questionArea.setColumns(20);
        questionArea.setEditable(false);
        questionArea.setFont(new java.awt.Font("Lucida Grande", 0, 14)); // NOI18N
        questionArea.setLineWrap(true);
        questionArea.setRows(5);
        questionArea.setWrapStyleWord(true);
        questionField.setViewportView(questionArea);

        textFieldPanel.add(questionField, java.awt.BorderLayout.CENTER);

        buttonPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        buttonPanel.setFont(new java.awt.Font("Lucida Grande", 0, 14));
        buttonPanel.setLayout(new java.awt.GridLayout(0, 1, 0, 3));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(textFieldPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(textFieldPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JTextArea questionArea;
    private javax.swing.JScrollPane questionField;
    private javax.swing.JPanel textFieldPanel;
    // End of variables declaration//GEN-END:variables

    void setTitle(String title) {
    }

    void setQuestion(String value) {
        this.questionArea.setText((value == null) ? "Question" : value);
    }

    /**
     * This method sets the initial answer options for multiple choice inclusive
     * type question.
     * @param answers
     */
    void setAnswers(List<GenericAnswer> answers) {
        for (Iterator<GenericAnswer> it = answers.iterator(); it.hasNext();) {
            GenericAnswer genericAnswer = it.next();
            JCheckBox button = new JCheckBox(genericAnswer.getValue());
            button.setFont((new Font("Lucida Grande", 0, 14)));
            buttonPanel.add(button);
            //buttonGroup1.add(button);
        }
    }

    /**
     * This method retrieves all the selected answers from the panel.
     * @return
     */
    String getSelectedAnswers() {
        Component[] components = buttonPanel.getComponents();
        ArrayList<String> answers = new ArrayList<String>();
        for (int i = 0; i < components.length; i++) {
            Component component = components[i];
            if (component instanceof JCheckBox) {
                JCheckBox check = (JCheckBox) component;
                if (check.isSelected()) {
                    answers.add(check.getText());
                }
            }
        }
        return Join(answers, ",");
    }

    /**
     * This method creates a complete comma separated string of the selected
     * check box answers.
     * @param answers
     * @param delimiter
     * @return
     */
    private String Join(ArrayList<String> answers, String delimiter) {
        if (answers.isEmpty()) {
            return "";
        }

        StringBuilder sb = new StringBuilder();

        for (String x : answers) {
            sb.append(x);
            sb.append(delimiter);
        }
        sb.delete(sb.length() - delimiter.length(), sb.length());
        return sb.toString();
    }
}
